<?php
namespace app\farm\model;

use app\BaseModel;
use think\facade\Db;

class BalanceWater extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_balance_water';

//
    protected $append = [

        'goods_title',

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-07-07 17:44
     * @功能说明:项目名称
     */
    public function getGoodsTitleAttr($value,$data){

        if(!empty($data['type'])&&!empty($data['order_id'])&&isset($data['add'])){

            $title = '';

            $balance_order_model = new BalanceOrder();
            //充值
            if($data['type']==1){

                $title = $balance_order_model->where(['id'=>$data['order_id']])->value('title');
            //认养
            }elseif($data['type']==2){

                $order_model = new ClaimOrder();

                $title = $order_model->where(['id'=>$data['order_id']])->value('goods_name');

            //养殖订单
            }elseif($data['type']==5){

                $shop_order_model = new BreedOrderGoods();

                $title = $shop_order_model->where(['order_id'=>$data['order_id']])->column('goods_name');

                $title = !empty($title)?implode(',',$title):'';
            //土地订单
            }elseif($data['type']==4){

                $order_model = new LandOrder();

                $title = $order_model->where(['id'=>$data['order_id']])->value('spe_name');

            }elseif($data['type']==7||$data['type']==11){

                $order_model = new ShopOrderGoods();

                $title = $order_model->where(['order_id'=>$data['order_id']])->column('goods_name');

                $title = !empty($title)?implode(',',$title):'';
            }

            return $title;

        }


    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-05 09:56
     * @功能说明:
     */
    public function sceneText($type){

        switch ($type){

            case 1:

                $text = '购买储值卡';

                break;
            case 2:

                $text = '认养订单';

                break;

            case 3:

                $text = '认养配送订单';

                break;

            case 4:

                $text = '配送订单退款';

                break;

            case 5:

                $text = '养殖订单';

                break;

            case 6:

                $text = '土地订单';

                break;

            case 7:

                $text = '商城交易支付';

                break;

            case 8:

                $text = '提现';

                break;
            case 9:

                $text = '土地配送订单';

                break;
            case 10:

                $text = '商城订单退款';

                break;
            case 11:

                $text = '商城交易收入';

                break;

            case 12:

                $text = '拒绝提现';

                break;

            case 13:

                $text = '认养订单';

                break;

            default:

                $text = '购买储值卡1';

                break;

        }

        return $text;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

//        if(){
//
//        }

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function indexList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['after_balance']  = round($v['after_balance'],2);

                $v['before_balance'] = round($v['before_balance'],2);

                $add_text = $v['add']==1?'+':'-';

                $buy_text = $v['add']==1?'退款':'消费';

                if($v['type']==1){

                    $buy_text = '';
                }

                $scene_text = $this->sceneText($v['type']);

                $v['text'] = $scene_text.$buy_text.'【'.$v['goods_title'].'】'.$add_text.'¥'.$v['price'].'现余额¥'.$v['after_balance'];

                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

            }
        }

        return $data;

    }




    /**
     * @author chenniang
     * @DataTime: 2022-02-09 11:14
     * @功能说明:增加流水并且修改用户余额
     */
    public function addWater($order,$type,$add=0){

        $user_model  = new \app\farm\model\User();

        $water_model = new BalanceWater();

        $user = $user_model->dataInfo(['id'=>$order['user_id']]);
        //如果是平台
        if(empty($user)){

            return true;
        }

        $dis = [

            'id'   => $user['id'],

            'lock' => $user['lock']

        ];

        $update  = [

            'lock' => $user['lock']+1
        ];

        if($add==0){

            $update['balance'] = $user['balance']-$order['pay_price'];
            //地主提现或者退款
            if(in_array($type,[8,10])){

                $update['wallet_cash']   = $user['wallet_cash']  - $order['pay_price'];

                $update['landlord_cash'] = $user['landlord_cash']- $order['pay_price'];
            }
            //如果总余额小于可提现余额 可提现余额也要减掉
            if($update['balance']<$user['wallet_cash']){

                $update['wallet_cash']   = $update['balance'];

                $update['landlord_cash'] = $update['balance'];
            }

        }else{

            $update['balance'] = $user['balance']+$order['pay_price'];
            //地主收入 需要增加可提现余额
            if($type==11||$type==12){

                $update['wallet_cash']   = $user['wallet_cash']+$order['pay_price'];

                $update['landlord_cash'] = $user['landlord_cash']+$order['pay_price'];

            }

        }
        //修改用户余额
        $res = $user_model->dataUpdate($dis,$update);

        if($res==0){

            return false;
        }
        //添加余额流水
        $insert = [

            'uniacid' => $order['uniacid'],

            'user_id' => $order['user_id'],

            'price'   => $order['pay_price'],

            'order_id'=> $order['id'],

            'add'     => $add,

            'type'    => $type,

            'before_balance' => $user['balance'],

            'after_balance'  => $add==0?$user['balance']-$order['pay_price']:$user['balance']+$order['pay_price'],
        ];

        $res = $water_model->dataAdd($insert);

        if($res==0){

            return false;

        }

        return true;

    }





    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }







}